<template>
  <VPage class="app-container">
    <VTable ref="tableRef" v-bind="tableOptins">
      <template #form="{ form }">
        <VGroup>
          <el-select style="max-width: 100px" v-model="form.orderRange.type" placeholder="请选择">
            <el-option v-for="item in orderOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
          <el-input v-model="form.orderRange.value" placeholder="请输入" class="w-40"></el-input>
        </VGroup>
        <select-personnel class="w-40" v-model="form.owner_id" placeholder="销售员"></select-personnel>
        <SelectLogistics v-model:logistics="form.logistics_id" v-model:channel="form.logistics_channel_id"></SelectLogistics>
        <SelectCountry class="w-30" v-model="form.country" placeholder="国家"></SelectCountry>
        <el-select class="selectClass w-30" v-model="form.order_type" placeholder="订单类型">
          <el-option v-for="item in options['deliverymanage.omsOrder']?.[0].orderType" :key="item.value"
            :label="item.label" :value="item.value"></el-option>
        </el-select>
        <el-select class="selectClass w-30" v-model="form.allot_status" placeholder="配货状态">
          <el-option v-for="item in options['deliverymanage.omsOrder']?.[0].allotStatusMap" :key="item.value"
            :label="item.label" :value="item.value"></el-option>
        </el-select>
        <VGroup>
          <el-select class="selectClass" v-model="form.timeRange.type" style="max-width: 100px">
            <el-option v-for="item in timeOption" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
          <el-date-picker v-model="form.timeRange.value" type="daterange" range-separator="至" start-placeholder="开始时间"
            end-placeholder="结束时间" value-format="YYYY-MM-DD HH:mm:ss" :default-time="defaultTime()" style="max-width: 300px" />
        </VGroup>
      </template>
      <template #toolbar_btns>
        <el-button type="primary" @click="handleDistribution()">批量配货</el-button>
      </template>
    </VTable>
    <ComModal ref="ComModalRef" @query="query"></ComModal>
  </VPage>
</template>
<script setup name="Deliver-goodsWait-list" lang="jsx">
import { ElMessage } from 'element-plus'
import { ref, reactive, toRefs } from 'vue'
import { getEnums } from '@/api/public'
import { queryWaitList, orderDistribution } from './api'
import ComModal from './components/ComModal.vue'
import SelectLogistics from '@/components/SelectLogistics'
import SelectCountry from '@/components/SelectCountry'

const timeOption = [
  { label: "创建时间", value: "create_time" },
  { label: "下单时间", value: "order_time" },
]

const orderOptions = [
  { label: "订单号", value: 'oms_order_no' },
  { label: "跟踪号", value: 'tracking_number' },
]

const tableRef = ref()
const tableOptins = reactive({
  rowConfig: { height: 60 },
  formConfig: {
    data: {
      orderRange: { type: 'oms_order_no', value: [], range: true },
      timeRange: { type: 'create_time', value: [], range: true },
    }
  },
  columns: [
    {
      type: 'checkbox', width: "40"
    },
    {
      type: "seq", title: '序号', width: 80
    },
    {
      field: 'oms_order_no', title: '订单号', width: 210
    },
    {
      field: 'tracking_number', title: '跟踪号', width: 180
    },
    {
      field: 'logistics_name', title: '物流商', width: 180
    },
    {
      field: 'logistics_type_name', title: '物流方式', width: 120
    },
    {
      field: 'allot_status_cn', title: '配货状态', width: 80
    },
    {
      field: 'reason', title: '原因', width: 160
    },
    {
      field: 'platform', title: '订单来源', width: 120
    },
    {
      field: 'order_time', title: '下单时间', width: 180
    },
    {
      field: 'create_time', title: '创建时间', width: 180
    },
    {
      field: 'warehouse_name', title: '仓库', width: 120
    },
    {
      field: 'owner_name_arr', title: '销售员', width: 120
    },
    {
      field: 'ship_remark', title: '备注', width: 120
    },
    {
      title: '操作', width: "120", fixed: "right", slots: {
        default: ({ row }) => {
          return <div class="table-option">
            <div>
              <el-link type="primary" underline={false} class="mr-2" onClick={() => open('view', row.id)}>查看</el-link>
              <el-link type="primary" underline={false} class="mr-2" onClick={() => handleDistribution(row.id)}>手动配货</el-link>
            </div>
          </div >
        }
      }
    }
  ],
  proxyConfig: {
    seq: true,
    props: {
      result: 'data',
      total: 'total',
    },
    ajax: {
      query: async ({ page, form }) => {
        const res = await queryWaitList({ ...form, ...page })
        return new Promise(function (resolve) {
          resolve({
            data: res.data,
            total: res.total
          })
        })
      }
    }
  }
})
const query = () => {
  tableRef.value.query()
}

const data = reactive({
  options: [],
})
const { options} = toRefs(data)

// 获取枚举类
const fetchOption = async () => {
  getEnums({
    version: "v1", "name": [
      "deliverymanage.omsOrder"
    ]
  }).then(res => {
    options.value = res.data
  })
}
fetchOption()


//获取已选中的行
const getSelectRow = () => {
  return tableRef?.value.$table.getCheckboxRecords()
}

const handleDistribution = async (id) => {
  const records = getSelectRow().map(i => {
    return i.id
  })
  if (!id) {
    if (records.length == 0) {
      ElMessage.error('请先选择行')
      return
    }
  }
  const ids = id ? [id] : records
  try {
    const res = await orderDistribution({
      id: ids
    })
    ElMessage.success('操作成功')
    query()
  } catch (e) {
    throw Error(e)
  }
}

const ComModalRef = ref()
const open = (type, id) => {
  ComModalRef.value.open(type, id)
}

</script>